package com.vortex.cloud.sample.enums;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Optional;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-10-14 周四
 * @time 8:33
 * @description
 */
@Getter
public enum Sex {

    MAN("1", "男"), WOMAN("0", "女"), UNKNOWN("2", "未知");

    private final String code;
    private final String desc;

    Sex(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public static Sex getByCode(String code) {
//        AssertUtil.isNotBlank(code, "枚举参数错误");
        if (StringUtils.isBlank(code)) {
            throw new RuntimeException("枚举参数错误");
        }

        Sex[] values = values();
        Optional<Sex> found = Arrays.stream(values).filter(it -> it.code.equals(code)).findAny();
        return found.isPresent() ? found.get() : null;
    }
}
